package com.bob.light.dialog

import android.app.Dialog
import android.content.Context
import android.os.Bundle
import android.view.View
import com.bob.light.R
import com.bob.light.setOnSingleClickListener
import kotlinx.android.synthetic.main.app_dialog_message.*

class MessageDialog(context: Context)
    : Dialog(context, R.style.AlertDialog_AppCompat_AlertDialogStyle),
        View.OnClickListener {

    private var mContent: String = ""

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    private fun initView() {
        tv_dialog_msg_content?.text = mContent
        tv_dialog_msg_cancel_btn.setOnSingleClickListener(this)
        tv_dialog_msg_confirm_btn.setOnSingleClickListener(this)
    }

    fun content(content: String): MessageDialog {
        this.mContent = content
        return this@MessageDialog
    }

    override fun onClick(v: View?) {
        when(v?.id) {
            R.id.tv_dialog_msg_cancel_btn -> {
                dismiss()
            }
            R.id.tv_dialog_msg_confirm_btn -> {
                confirmClickListener?.invoke()
                dismiss()
            }
        }
    }

    private var confirmClickListener: (() -> Unit)? = null

    fun setOnConfirmClickListener(confirmClickListener: () -> Unit): MessageDialog {
        this@MessageDialog.confirmClickListener = confirmClickListener
        return this@MessageDialog
    }

    init {
        setContentView(R.layout.app_dialog_message)
    }

}